<template>
  <div>
    <h1>退补货订单处理</h1>
    <!-- 搜索框 -->
    <div class="add_info">
      <el-button type="primary" class="addbtn" @click="add()"
        >添加订单</el-button
      >
    </div>
    <!-- 添加弹框 -->
    <div class="editor" v-show="editActive">
      <p>
        <span>客户名称：</span>
        <input type="text" v-model="editlist.name" class="inp" />
      </p>
      <p>
        <span>商品名称：</span><input v-model="editlist.username" class="inp" />
      </p>
      <p>
        <span>商品数量：</span
        ><input type="text" v-model="editlist.number" class="inp" />
      </p>
      <p>
        <span>发货地址：</span
        ><input type="text" v-model="editlist.address" class="inp" />
      </p>
      <p>
        <span>退补原因：</span
        ><input type="text" v-model="editlist.why" class="inp" />
      </p>

      <div class="edit_btn">
        <el-button type="success" @click="determine()">确定</el-button>
        <el-button type="danger" @click="editChange()">取消</el-button>
      </div>
    </div>
    <!-- 表单 -->
    <div class="contariner">
      <div class="tab">
        <el-table :data="myList" stripe style="width: 100%" text-align="">
          <tr class="tr">
            <el-table-column prop="id" label="" width="0"> </el-table-column>
            <el-table-column label="ID" type="index"> </el-table-column>
            <el-table-column prop="name" label="用户名" width="100">
            </el-table-column>
            <el-table-column prop="username" label="商品名称" width="100">
            </el-table-column>
            <el-table-column prop="number" label="商品数量" width="80">
            </el-table-column>
            <el-table-column prop="address" label="发货地址" width="280">
            </el-table-column>
            <el-table-column prop="why" label="退补发原因" width="100">
            </el-table-column>
            <el-table-column label="操作" width="180px">
              <template slot-scope="scope">
                <el-button size="mini" @click="edit(scope.row, scope.$index)"
                  >编辑</el-button
                >
                <el-button size="mini" type="danger" @click="del(scope.$index)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </tr>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script>
import api from "../../api/index";
export default {
  name: "",
  components: {},
  data() {
    return {
      indexMethod: "",
      search: "",
      input: "",
      //显隐判断
      editActive: false,
      //确定按钮时的判断
      keyword: 0,
      //使用修改按钮时对应的行数
      selected: 0,
      //临时用的数据存储
      editlist: {
        name: "",
        username: "",
        number: "",
        address: "",
        why: "",
        vocation: "",
      },

      //展示用的数据
      myList: [],
    };
  },
  created() {
    api.order.getListData().then((res) => {
      this.myList = res.data;
    });
  },
  methods: {
    // 添加按钮
    add() {
      // 将编辑框置空
      this.editlist = {};
      // 0 在确定按钮进行判断
      this.keyword = 0;
      this.editChange();
    },
    // 修改按钮
    edit(item, index) {
      this.editlist = {
        ...item,
      };
      this.keyword = 1;
      this.selected = index;
      // 处理完数据后可以让编辑框显示了
      this.editChange();
    },
    // 删除按钮
    del(index) {
      this.myList.splice(index, 1);
    },
    // 编辑框的确定按钮
    determine() {
      // 判断是添加还是修改 0是添加 1是修改

      if (this.keyword === 0) {
        this.myList.push(this.editlist);
      } else {
        this.$set(this.myList, this.selected, this.editlist);
      }
      this.editChange();
    },
    // 编辑框的显隐
    editChange() {
      this.editActive = !this.editActive;
    },
  },
};
</script>

<style scoped lang="less">
h1 {
  text-align: center;
  font-weight: bold;
  font-size: 30px;
}
//搜索框
.add_info {
  margin-left: 50px;

  .addbtn {
    width: 100px;
    height: 40px;
    border-radius: 16px; //圆角
  }
}
//表格
.contariner {
  .tab {
    margin: 30px 50px;
  }
}
//弹框
.editor {
  width: 500px;
  display: flex;
  flex: 1;
  flex-direction: column;
  align-content: center;
  justify-content: space-between;
  text-align: center;
  line-height: 50px;
  border: 1px solid #000;
  height: 330px;
  margin: 60px;
  border-radius: 30px;
  background-color: #fff;
  span {
    color: #696969;
  }
  input {
    margin-top: 20px;
  }
  //点击input后，边框变颜色
  .inp:focus {
    outline: none;

    border: 1px solid #ccc;
  }
  .edit_btn {
    text-align: center;
  }
}
//表单
.contariner {
  .tab {
    width: 1000px;
    display: flex;
    flex: 1;
    line-height: 50px;
    padding-right: 30px;
  }
  .tr {
    th {
    }
  }
  .tr1 {
  }
}
</style>
